package com.irdstudio.apicenter.frame.tag.bootstrap;

import java.util.Vector;

import javax.servlet.jsp.JspException;

import com.irdstudio.apicenter.console.controller.MacmsContext;
import com.irdstudio.apicenter.frame.core.jdbc.container.DataList;
import com.irdstudio.apicenter.frame.core.jdbc.container.PageInfo;
import com.irdstudio.apicenter.frame.tag.ParentTagSupport;

/**
 * 数据表格标签(bootstrap风格)
 * 
 * @author guangming.li
 * @date 2015-06-14
 * @version 1.0
 */
public class TTable extends ParentTagSupport {

	/* 标签指定的数据集名称 */
	private String dataListName = null;

	/* 分页标志,默认按分页显示 */
	private boolean pageMode = true;

	/* 分页查询地址 */
	private String url = "";

	private static final long serialVersionUID = 1L;

	/* 字段信息存储 */
	protected Vector<TAbstractText> fields = new Vector<TAbstractText>();

	@Override
	public int doStartTag() throws JspException {
		fields.clear();
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {

		StringBuffer sb = new StringBuffer();

		/* TABLE */
		sb
				.append("<table class=\"table table-bordered\" width=\"100%\" cellspacing=\"1\" cellpadding=\"0\" border=\"0\">\n");

		sb.append("<thead>\n");
		sb.append("		<tr  class=\"active\">\n");

		for (int i = 0; this.fields != null && i < this.fields.size(); i++) {
			TAbstractText column = this.fields.elementAt(i);
			sb.append("			<td align=\"").append(column.getAlign())
					.append("\">").append(column.getLabel()).append("</td>\n");
		}
		sb.append("		</tr>\n");
		sb.append("</thead>\n");

		// 生成表格body
		sb.append("<tbody>\n");
		MacmsContext context = (MacmsContext) pageContext.getRequest()
				.getAttribute("context");
		DataList dataList = context.getDataList(this.getDataListName());
		for (int j = 0; j < dataList.size(); j++) {
			sb.append("	<tr>\n");
			for (int i = 0; this.fields != null && i < this.fields.size(); i++) {
				TAbstractText column = this.fields.elementAt(i);

				column.renderTag(sb, dataList.get(j));

			}
			sb.append("	\n</tr>\n");
		}
		sb.append("	</tbody>\n");
		sb.append("</table>\n");

		if (pageMode && context.getPageInfo(this.getDataListName()) != null) {
			// 如果分页,生成分页相关的HTML
			PageInfo pgInfo = context.getPageInfo(this.getDataListName());
			sb
					.append("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n");
			sb.append("	<tr>\n");
			sb.append("		<td align=\"center\" >\n");
			sb
					.append(
							"		  <input type=\"hidden\" id=\"pageSize\" name=\"pageSize\" value=\"")
					.append(pgInfo.pageSize).append("\"/> \n");
			sb
					.append(
							"		  <input type=\"hidden\" id=\"pageNo\" name=\"pageNo\" value=\"")
					.append(pgInfo.pageIdx).append("\"/> \n");
			sb
					.append(
							"	      <input type=\"hidden\" id=\"totalPage\" name=\"totalPage\" value=\"")
					.append(pgInfo.totalPage).append("\"/> \n");

			sb
					.append(
							"		 <input type=\"hidden\" id=\"recordSize\" name=\"totalCount\" value=\"")
					.append(pgInfo.recordSize).append("\"/>\n");
			sb.append("共").append(pgInfo.recordSize).append("条记录&nbsp; 每页")
					.append(pgInfo.pageSize).append("条&nbsp;");

			// 处理首页按钮
			sb
					.append("<input type=\"button\"  class=\"btn btn-default btn-sm\"  name=\"fristPage\" value=\"首 页\" ");
			if (pgInfo.pageIdx > 1) {
				sb.append("onclick=\"MACMSTools.jumpPage(1);\" >\n");
			} else {
				sb.append("disabled=\"disabled\" >\n");
			}

			// 处理上一页按钮
			sb
					.append("<input type=\"button\"  class=\"btn btn-default btn-sm\"  name=\"frontPage\" value=\"上一页\" ");
			if (pgInfo.pageIdx > 1) {
				sb.append("onclick=\"MACMSTools.jumpPage(").append(
						pgInfo.pageIdx - 1).append(");\" />\n");
			} else {
				sb.append("disabled=\"disabled\" >\n");
			}

			// 处理下一页
			sb
					.append("<input type=\"button\"  class=\"btn btn-default btn-sm\"  name=\"nextPage\" value=\"下一页\" ");
			if (pgInfo.totalPage > pgInfo.pageIdx) {
				sb.append("onclick=\"MACMSTools.jumpPage(").append(
						pgInfo.pageIdx + 1).append(");\"/>\n");
			} else {
				sb.append("disabled=\"disabled\" >\n");
			}

			// 处理尾页
			sb
					.append("<input type=\"button\"  class=\"btn btn-default btn-sm\"  name=\"lastPage\" value=\"尾 页\" ");
			if (pgInfo.pageIdx < pgInfo.totalPage) {
				sb.append("onclick=\"MACMSTools.jumpPage(").append(
						pgInfo.totalPage).append(");\" > ");
			} else {
				sb.append("disabled=\"disabled\" >");
			}
			sb
					.append("&nbsp; 当前")
					.append(pgInfo.pageIdx)
					.append("/")
					.append(pgInfo.totalPage)
					.append(
							" 页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n");
			sb.append("		</td>\n");
			sb.append("	</tr>\n");
			sb.append("</table>\n");
		}

		outputContent(sb.toString());

		return SKIP_BODY;

	}

	public void setUrl(String url) {
		this.url = url;
	}

	private Object getUrl() {
		return this.url;
	}

	public void addDataField(TAbstractText tText) {
		this.fields.addElement(tText);
	}

	public String getDataListName() {
		return dataListName;
	}

	public void setDataListName(String dataListName) {
		this.dataListName = dataListName;
	}

	public boolean getPageMode() {
		return pageMode;
	}

	public void setPageMode(boolean pageMode) {
		this.pageMode = pageMode;
	}
}
